package com.dy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.common.R;
import com.dy.entity.Category;
import com.dy.entity.Dish;
import com.dy.entity.Setmeal;
import com.dy.mapper.CategoryMapper;
import com.dy.mapper.DishMapper;
import com.dy.mapper.SetmealMapper;
import com.dy.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * ClassName:CategoryServiceImpl
 * Package:com.dy.service.impl
 * Description:
 *
 * @Date:2022/8/4 8:57
 * @Author:dy
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * @Author dy
     * @Description: 根据分类名查询
     */
    @Override
    public Category getCategoryByName(String name) {
        Category category =  categoryMapper.getCategoryByName(name);
        return category;
    }

    @Override
    public R<String> delete(Long ids) {
        //判断该分类 是否还有关联菜品
        LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ids != null,Dish::getCategoryId,ids);
        Integer count1 = dishMapper.selectCount(queryWrapper1);
        if(count1 > 0){
            return R.error("该分类还有关联菜品，请先删除关联的菜品。(ง •_•)ง");
        }

        //判断该分类 是否还有关联套餐
        LambdaQueryWrapper<Setmeal> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(ids != null,Setmeal::getCategoryId,ids);
        Integer count2 = setmealMapper.selectCount(queryWrapper2);
        if(count2 > 0){
            return R.error("该分类还有关联套餐，请先删除关联的套餐。(ง •_•)ง");
        }

        int i = categoryMapper.deleteById(ids);
        if(i > 0){
            return R.success("删除成功");
        }
        return R.error("服务故障");
    }


}
